package com.sheepone.util.weka;

import weka.classifiers.AbstractClassifier;
import weka.core.Instances;

/**
 * @author Miss.杨
 * @description
 * @since 2024/3/4 - 21:36
 */
public class FitCurveHolder {
    private final AbstractClassifier classifier;

    private final int independentVariableIndex;

    private final Instances originData;

    public FitCurveHolder(double[][] data) {
        this(data, 0, 1);
    }
    public FitCurveHolder(Double[][] data) {
        this(data, 0, 1);
    }

    public FitCurveHolder(Instances instances) {
        instances.setClassIndex(1);
        this.originData = instances;
        this.independentVariableIndex = 0;
        this.classifier = M5PUtil.build(originData);
    }

    public FitCurveHolder(double[][] data, int independentVariableIndex, int dependentVariableIndex) {
        this.originData = WekaUtil.convertToInstances(data, dependentVariableIndex);
        this.independentVariableIndex = independentVariableIndex;
        this.classifier = M5PUtil.build(originData);
    }
    public FitCurveHolder(Instances instances, int independentVariableIndex, int dependentVariableIndex) {
        instances.setClassIndex(dependentVariableIndex);
        this.originData = instances;
        this.independentVariableIndex = independentVariableIndex;
        this.classifier = M5PUtil.build(originData);
    }


    public FitCurveHolder(Double[][] data, int independentVariableIndex, int dependentVariableIndex) {
        this.originData = WekaUtil.convertToInstances(data, dependentVariableIndex);
        this.independentVariableIndex = independentVariableIndex;
        this.classifier = M5PUtil.build(originData);
    }

    /**
     * 预测自变量在改拟合曲线的结果因变量值
     *
     * @param x 自变量
     * @return
     */
    public double predict(double x) {
        return M5PUtil.predict(classifier, x, independentVariableIndex, 2,originData);
    }

    public boolean saveInstances(String filePath) {
        return WekaUtil.saveInstances(originData, filePath);
    }
}
